import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Req,
  HttpStatus,
  HttpCode,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { NotificationService } from './notification.service';
import { CreateNotificationDto, NotificationResponseDto, UpdateNotificationDto } from './dto';
import { Permissions } from '../../decorators/permissions.decorator';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { plainToClass } from 'class-transformer';

@ApiTags('通知')
@Controller('notifications')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth()
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @Get()
  @Permissions('notification:read')
  @ApiOperation({ summary: '获取当前用户的通知列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认为1' })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量，默认为10' })
  @ApiQuery({ name: 'isRead', required: false, description: '是否已读，不传则获取全部' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '获取成功',
    type: [NotificationResponseDto],
  })
  async getUserNotifications(
    @Req() req,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
    @Query('isRead') isRead?: boolean,
  ) {
    const userId = req.user.id;
    const { notifications, total } = await this.notificationService.getUserNotifications(
      userId,
      { page, limit, isRead },
    );
    
    return {
      data: notifications.map(notification => 
        plainToClass(NotificationResponseDto, notification)
      ),
      meta: {
        total,
        page: page || 1,
        limit: limit || 10,
      },
    };
  }

  @Get('unread-count')
  @Permissions('notification:read')
  @ApiOperation({ summary: '获取当前用户未读通知数量' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '获取成功',
    schema: {
      type: 'object',
      properties: {
        count: {
          type: 'number',
          description: '未读通知数量',
        },
      },
    },
  })
  async getUnreadCount(@Req() req) {
    const userId = req.user.id;
    const { total } = await this.notificationService.getUserNotifications(
      userId,
      { isRead: false },
    );
    
    return { count: total };
  }

  @Get(':id')
  @Permissions('notification:read')
  @ApiOperation({ summary: '获取通知详情' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '获取成功',
    type: NotificationResponseDto,
  })
  async getNotification(@Param('id') id: string, @Req() req) {
    const userId = req.user.id;
    const notification = await this.notificationService.getNotificationById(id, userId);
    return plainToClass(NotificationResponseDto, notification);
  }

  @Patch(':id/read')
  @Permissions('notification:update')
  @ApiOperation({ summary: '将通知标记为已读' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '标记成功',
    type: NotificationResponseDto,
  })
  async markAsRead(@Param('id') id: string, @Req() req) {
    const userId = req.user.id;
    const notification = await this.notificationService.markAsRead(id, userId);
    return plainToClass(NotificationResponseDto, notification);
  }

  @Patch('read-all')
  @Permissions('notification:update')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '将所有通知标记为已读' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '标记成功' })
  async markAllAsRead(@Req() req) {
    const userId = req.user.id;
    await this.notificationService.markAllAsRead(userId);
  }

  @Delete(':id')
  @Permissions('notification:delete')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '删除通知' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '删除成功' })
  async deleteNotification(@Param('id') id: string, @Req() req) {
    const userId = req.user.id;
    await this.notificationService.deleteNotification(id, userId);
  }
} 